// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.WindowsAPICodePack.Taskbar;

namespace TaskbarConcepts
{
    /// <summary>
    /// Interaction logic for ProgressBar.xaml
    /// </summary>
    public partial class ProgressBar : UserControl
    {
        public ProgressBar()
        {
            InitializeComponent();
            this.Loaded += ProgressBar_Loaded;
            this.Unloaded += ProgressBar_Unloaded;
        }

        private void ProgressBar_Unloaded(object sender, RoutedEventArgs e)
        {
            TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress);
        }

        private void ProgressBar_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.ProgressStateSelection.ItemsSource == null)
            {
                this.ProgressStateSelection.ItemsSource = Enum.GetValues(typeof(TaskbarProgressBarState));
                ProgressStateSelection.SelectedItem = TaskbarProgressBarState.NoProgress;
            }
        }

        private void progressSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            UpdateProgress();
        }

        //This method is part of the lab. If we are asked to show a progress bar,
        //choose the appropriate TaskbarProgressState value and use the UpdateProgress
        //method to perform the update.
        private void ShowProgressBar_Click(object sender, RoutedEventArgs e)
        {
            if (ShowProgressBar.IsChecked.Value)
            {
                ProgressStateSelection.SelectedItem = TaskbarProgressBarState.Normal;
            }
            else
            {
                ProgressStateSelection.SelectedItem = TaskbarProgressBarState.NoProgress;
            }
            UpdateProgress();
        }

        //This method is part of the lab. When the progress bar state changes,
        //synchronize it with the checkbox and use the UpdateProgress method to
        //perform the update.
        private void ProgressStateSelection_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if ((TaskbarProgressBarState)ProgressStateSelection.SelectedItem == TaskbarProgressBarState.NoProgress)
            {
                ShowProgressBar.IsChecked = false;
            }
            else
            {
                ShowProgressBar.IsChecked = true;
            }
            UpdateProgress();
        }

        //This method is part of the lab. When asked to update the progress state and
        //we should indeed display a taskbar progress bar, use the TaskbarManager's
        //SetProgressValue and SetProgressState methods to update the progress bar state.
        private void UpdateProgress()
        {
            if (ShowProgressBar.IsChecked.Value)
            {
                TaskbarManager.Instance.SetProgressValue((int)progressSlider.Value, 100);
                TaskbarManager.Instance.SetProgressState((TaskbarProgressBarState)ProgressStateSelection.SelectedItem);
            }
            else
            {
                TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress);
            }
        }
    }
}